using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Possan.Graphics.Filters
{
	public class Crop : IBaseFilter
	{
		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}

			  
		private int _ow;
		/// <summary>
		/// Output image width in pixels
		/// </summary>
		public int OutputWidth
		{
			get
			{
				     return _ow;
			}
			set
			{
				_ow = value;
			}
		}

		private int _oh;

		/// <summary>
		/// Output image height in pixels
		/// </summary>
		public int OutputHeight
		{
			get {      return _oh; }
			set { _oh = value; }
		}

		private int _ax;

		/// <summary>
		/// Horizontal alignment in percent, 50 is centered, 0 is top, 100 is bottom
		/// </summary>
		public int AlignX
		{
			get {      return _ax; }
			set { _ax = value; }
		}


		private int _ay;

		/// <summary>
		/// Vertical alignment in percent, 50 is centered, 0 is left, 100 is right
		/// </summary>
		public int AlignY
		{
			get {      return _ay; }
			set { _ay = value; }
		}


		public Crop()
		{
			OutputWidth = 500;
			OutputHeight = 500;
			AlignX = 50;
			AlignY = 50;
		}

		public Bitmap Filter(Bitmap oldbitmap)
		{
			Bitmap neu = new Bitmap(OutputWidth, OutputHeight);
			neu.SetResolution(oldbitmap.HorizontalResolution, oldbitmap.VerticalResolution);

			float oldw = (float)oldbitmap.Width;
			float oldh = (float)oldbitmap.Height;

			float neww = (float)OutputWidth;
			float newh = (float)OutputHeight;

			float mxo = neww - oldw;
			float myo = newh - oldh;
			//		if (mxo < 0 &&  && )
			//		mxo = 0;
			//	if (myo < 0  )
			//	myo = 0;

			float ox = (neww / 2.0f) + mxo * (float)(AlignX - 50) / 100.0f;
			float oy = (newh / 2.0f) + myo * (float)(AlignY - 50) / 100.0f;

			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(neu);
			g.InterpolationMode = InterpolationMode.HighQualityBicubic;
			g.CompositingQuality = CompositingQuality.HighQuality;
			g.ResetTransform();

			g.TranslateTransform(-oldw / 2.0f, -oldh / 2.0f);
			g.TranslateTransform(ox, oy);

			g.DrawImage(oldbitmap, 0, 0);

			     return neu;
		}

		public void Dispose()
		{
		}
					    
	}
}





